package org.example.component;

import org.example.strategy.ShardingDBConfig;
import org.example.strategy.ShardingTableConfig;
import org.example.util.CommonUtil;
import org.springframework.stereotype.Component;

/**
 * @description: 短链码组件
 * @author: 谢超
 * @date: 2023/8/15 9:22
 */
@Component
public class ShortLinkComponent {

    /**
     * 生成短链码
     *
     * @param str
     * @return
     */
    public String createShortLinkCode(String str) {
        // 获取murmurHash值
        long hashCode = CommonUtil.murmurHash32(str);

        // 将其转为62位进制
        String code = CommonUtil.codeToBase62(hashCode);

        // 返回shardingDBConfig和shardingTableConfig拼接出来的短链码
        return ShardingDBConfig.getRandomDBPrefix(code) + code + ShardingTableConfig.getRandomTableSuffix(code);
    }


}
